namespace Example.Models
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.UI.Html;

    [XmlRoot("container")]
    public sealed class RegistrationForm : FormContainer
    {
        public override void Complete(FormSession session)
        {
            if (null == session)
            {
                throw new ArgumentNullException("session");
            }

            RegisteredUser user = new RegisteredUser
            {
                Urn = "urn://user/" + session.Data["email"].ToLowerInvariant(),
                Email =
                {
                    Address = session.Data["email"]
                },
                Name =
                {
                    Given = session.Data["name-given"],
                    Family = session.Data["name-family"]
                }
            };
            
            user.Store<RegisteredUser>();
            user.SetAuthenticationCookie<RegisteredUser>(false);

            session.Data.Completed = true;
            session.Store();
        }

        public override T Template<T>()
        {
            return Record.Retrieve<T>(new AbsoluteUri("urn://simplewebservices.org/form/registration"));
        }
    }
}